package de.rayman2200.converter.components;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.ImageIcon;
import javax.swing.JComponent;


public class JDropableComponent extends JComponent implements ComponentListener
{

  private static final long serialVersionUID = -3072978732941239567L;

  private Point point = new Point();

  private Dimension compSize = getSize();

  private ImageIcon img;

  public JDropableComponent(ImageIcon img, DropTargetListener dtl)
  {
    new DropTarget(this, dtl);
    addComponentListener(this);
    addMouseListener(new ContextMenu());
    this.img = img;
  }

  @Override
  public void paint(Graphics g)
  {
    g.drawImage(img.getImage(), point.x, point.y, compSize.width, compSize.height, this);
  }

  @Override
  public Dimension getPreferredSize()
  {
    return new Dimension(img.getIconWidth(), img.getIconHeight());
  }

  @Override
  public void componentHidden(ComponentEvent arg0)
  {}

  @Override
  public void componentMoved(ComponentEvent arg0)
  {}

  @Override
  public void componentResized(ComponentEvent arg0)
  {
    compSize = arg0.getComponent().getSize();
  }

  @Override
  public void componentShown(ComponentEvent arg0)
  {
    compSize = arg0.getComponent().getSize();
  }
}
